@isTest
private class CaseBlacklistHandler_Test
{
    public static testMethod void testCaseBlacklistHandler()
    {
        
        // test we only add one row     
        List<CaseBlackList__c> bvecs = [Select Id From CaseBlackList__c];
        Integer beforeSize = bvecs.size();
        CaseBlacklistHandler.add('unittest@salesforce.com');
        List<CaseBlackList__c> avecs = [Select Id From CaseBlackList__c];
        System.assertEquals(beforeSize+1, avecs.size());
        
        // was it added to EmailVelocityIgnore?
        List<EmailVelocityIgnore__c> evil = EmailVelocityIgnoreHandler.get('unittest@salesforce.com','unittest@salesforce.com');
        System.assertEquals(evil.size(), 1);
        System.assertEquals(evil[0].Threshold__c, 0);
        System.assertEquals(evil[0].IsActive__c, true);
        
        // was it added correctly?
        List<CaseBlackList__c> added = CaseBlacklistHandler.get('unittest@salesforce.com');
        System.assertEquals(1, added.size());
        CaseBlackList__c item = added[0];
        System.assertEquals(item.AutoCloseCase__c, false);
        System.assertEquals(item.NoAutoreply__c, false);
        System.assertEquals(item.Active__c, true);
               
        // do we only insert one row with those addresses?
        CaseBlacklistHandler.add('unittest@salesforce.com');
        List<CaseBlackList__c> avecs2 = [Select Id From CaseBlackList__c];
        System.assertEquals(avecs2.size(), avecs.size());
        added = CaseBlacklistHandler.get('unittest@salesforce.com');
        item = added[0];
        System.assertEquals(item.AutoCloseCase__c, false);
        System.assertEquals(item.NoAutoreply__c, false);
        System.assertEquals(item.Active__c, true);
        
        // add a new one
        CaseBlacklistHandler.add('unittest2@salesforce.com');
        CaseBlacklistHandler.edit('unittest2@salesforce.com', false, true, true);
        // does disable work?
        List<CaseBlackList__c>all = CaseBlacklistHandler.getBlackList();
        List<CaseBlackList__c> active = CaseBlacklistHandler.getActive();
        Integer preAllSize = all.size();
        Integer preActiveSize = active.size();
        CaseBlacklistHandler.disable('unittest2@salesforce.com');
        active = CaseBlacklistHandler.getActive();
        Integer postActiveSize = active.size();
        System.assertEquals(postActiveSize, preActiveSize-1);
        evil = EmailVelocityIgnoreHandler.get('unittest2@salesforce.com','unittest2@salesforce.com');
        System.assertEquals(evil[0].IsActive__c, false);
        
        
        // does enable work?
        all = CaseBlacklistHandler.getBlackList();
        active = CaseBlacklistHandler.getActive();
        preAllSize = all.size();
        preActiveSize = active.size();
        CaseBlacklistHandler.add('unittest2@salesforce.com');
        CaseBlacklistHandler.enable('unittest2@salesforce.com');
        active = CaseBlacklistHandler.getActive();
        postActiveSize = active.size();
        System.assertEquals(postActiveSize, preActiveSize+1);
        // in EmailVelocityIgnore too?
        evil = EmailVelocityIgnoreHandler.get('unittest2@salesforce.com','unittest2@salesforce.com');
        System.assertEquals(evil.size(), 1); 
        System.assertEquals(evil[0].IsActive__c, true);
               
        
               
        // does edit work?
        CaseBlacklistHandler.edit('unittest2@salesforce.com', true, true, true);
        item = CaseBlacklistHandler.get('unittest2@salesforce.com')[0];
        System.assertEquals(item.NoAutoreply__c, true);
        System.assertEquals(item.AutoCloseCase__c, true);
        // editing isactive to true should enable entry in EmailVelocityIgnore
        evil = EmailVelocityIgnoreHandler.get('unittest2@salesforce.com','unittest2@salesforce.com');
        System.assertEquals(evil[0].IsActive__c, true);
        
        CaseBlacklistHandler.edit('unittest2@salesforce.com', true, false, true);
        // editing noreply to false should disable entry in EmailVelocityIgnore
        evil = EmailVelocityIgnoreHandler.get('unittest2@salesforce.com','unittest2@salesforce.com');
        System.assertEquals(evil[0].IsActive__c, false);
        
        CaseBlacklistHandler.edit('unittest2@salesforce.com', true, true, true);
        CaseBlacklistHandler.edit('unittest2@salesforce.com', true, true, false);
        // editing isactive to false should disable entry in EmailVelocityIgnore
        evil = EmailVelocityIgnoreHandler.get('unittest2@salesforce.com','unittest2@salesforce.com');
        System.assertEquals(evil[0].IsActive__c, false);
        
        // test that we throw if we discover a bad state
        EmailVelocityIgnoreHandler.enable('unittest3@salesforce.com', 'unittest3@salesforce.com', 1);
        try 
        {
            CaseBlacklistHandler.add('unittest3@salesforce.com');
            System.assertEquals(true, false);
        }
        catch (CaseBlacklistHandler.CaseBlacklistException e){}
        System.assertEquals(true, true);
                   
    }
}